// pages/user/user.js

const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatarUrl: defaultAvatarUrl,
    name: "",
    openid: ""
  },

  onChooseAvatar(e) {
    const {
      avatarUrl
    } = e.detail
    this.setData({
      avatarUrl,
    })
  },

  nameChange(e) {
    console.log(123456);
    console.log(e.detail.value);
  },
  jump() {
    wx.switchTab({
      url: '../list/list',
    })
  },
  async submit(e) {

    app.globalData.name = e.detail.value.username;
    // if (app.globalData.openid && e.detail.value.username && this.data.avatarUrl) {

    if (app.globalData.openid && e.detail.value.username && this.data.avatarUrl) {

      wx.request({
        url: 'http://localhost:3000/login',
        method: "POST",
        data: {
          avatarUrl: this.data.avatarUrl,
          nickname: e.detail.value.username,
          openid: app.globalData.openid
        },
        success: res => {
          console.log(res);
        }
      })
      wx.showToast({
        title: '登陆成功',
        icon: 'success',
        duration: 2000 //持续的时间
      })
      setTimeout(this.jump, 1000)

    } else if (!e.detail.value.username) {
      wx.showModal({
        title: '提示',
        content: '请输入昵称',
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this;
    console.log(app.globalData.openid);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})